package com.zhentao.payment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhentao.payment.entity.RefundOrder;
import com.zhentao.payment.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 退款控制器
 * 处理退款相关的请求
 */
@RestController
@RequestMapping("/api/refund")
public class RefundController {
    @Autowired
    private RefundService refundService;

    /**
     * 创建退款申请
     *
     * @param params 包含以下参数：
     *              - orderNo: 原支付订单编号
     *              - amount: 退款金额
     *              - reason: 退款原因
     * @return 返回创建的退款记录信息
     */
    @PostMapping("/create")
    public ResponseEntity<RefundOrder> createRefund(@RequestBody Map<String, Object> params) {
        String orderNo = params.get("orderNo").toString();
        Double amount = Double.parseDouble(params.get("amount").toString());
        String reason = params.get("reason").toString();

        RefundOrder refund = refundService.createRefund(orderNo, amount, reason);
        return ResponseEntity.ok(refund);
    }

    /**
     * 查询退款记录
     *
     * @param refundNo 退款单号
     * @return 返回退款记录信息
     */
    @GetMapping("/query/{refundNo}")
    public ResponseEntity<RefundOrder> queryRefund(@PathVariable String refundNo) {
        RefundOrder refund = refundService.queryRefund(refundNo);
        return ResponseEntity.ok(refund);
    }

    /**
     * 分页查询退款记录
     */
    @GetMapping("/query")
    public ResponseEntity<IPage<RefundOrder>> queryRefundList(
            @RequestParam(required = false) String refundNo,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        IPage<RefundOrder> result = refundService.queryRefundList(refundNo, orderNo, status, page, size);
        return ResponseEntity.ok(result);
    }

    /**
     * 同意退款申请
     */
    @PostMapping("/approve/{refundNo}")
    public ResponseEntity<Void> approveRefund(@PathVariable String refundNo) {
        refundService.approveRefund(refundNo);
        return ResponseEntity.ok().build();
    }

    /**
     * 拒绝退款申请
     */
    @PostMapping("/reject/{refundNo}")
    public ResponseEntity<Void> rejectRefund(@PathVariable String refundNo) {
        refundService.rejectRefund(refundNo);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取最近退款记录
     */
    @GetMapping("/recent")
    public ResponseEntity<List<RefundOrder>> getRecentRefunds() {
        LambdaQueryWrapper<RefundOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(RefundOrder::getCreateTime)
               .last("LIMIT 10");
        return ResponseEntity.ok(refundService.list(wrapper));
    }
}
